import { createVNode as _createVNode, mergeProps as _mergeProps } from "vue";
import { computed, defineComponent, ref, watch } from "vue";
// pickerShareProps为picker中分享props, pickerInheritKeys为shareProp中keys, sharedProps为dataPicker中分享props 
import { formatValueRange, genOptions, pickerInheritKeys, sharedProps } from "../date-picker/utils.mjs";
import { pick, extend, isSameValue, makeNumericProp, createNamespace } from "../utils/index.mjs";
import { useExpose } from "../composables/use-expose.mjs";
import { Picker } from "../picker/index.mjs";
const [name] = createNamespace("time-picker");
// 24小时时间格式校验
const validateTime = (val) => /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/.test(val);
const fullColumns = ["hour", "minute", "second"];
const timePickerProps = extend({}, sharedProps, {
  // 单列时间限制范围
  minHour: makeNumericProp(0),
  maxHour: makeNumericProp(23),
  minMinute: makeNumericProp(0),
  maxMinute: makeNumericProp(59),
  minSecond: makeNumericProp(0),
  maxSecond: makeNumericProp(59),
  // 整体时间范围 minTime maxTime
  // 设置后 单独列的限制范围会失效
  minTime: {
    type: String,
    validator: validateTime
  },
  maxTime: {
    type: String,
    validator: validateTime
  },
  columnsType: { // 展示列['hour', 'minute', 'second']
    type: Array,
    default: () => ["hour", "minute"]
  },
  filter: Function
});
var stdin_default = defineComponent({
  name,
  props: timePickerProps,
  emits: ["confirm", "cancel", "change", "update:modelValue"],
  setup(props, {
    emit,
    slots
  }) {
    const currentValues = ref(props.modelValue);

    const pickerRef = ref();
    const getValidTime = (time) => {
      const timeLimitArr = time.split(":");
      return fullColumns.map((col, i) => props.columnsType.includes(col) ? timeLimitArr[i] : "00");
    };

    // 向外暴露的 确认  获取选中数据方法
    const confirm = () => {
      var _a;
      return (_a = pickerRef.value) == null ? void 0 : _a.confirm();
    };
    const getSelectedTime = () => currentValues.value;

    // 通过props.mixHour props.minTime props.columnsType currentValues 等计算picker展示所需的数据columns
    const columns = computed(() => {
      let {
        minHour,
        maxHour,
        minMinute,
        maxMinute,
        minSecond,
        maxSecond
      } = props;
      // props.minTime || props.maxTime 会覆盖 各列的设定值
      if (props.minTime || props.maxTime) {
        // 从currentValues中获取当前 时分秒
        const fullTime = {
          hour: 0,
          minute: 0,
          second: 0
        };
        props.columnsType.forEach((col, i) => {
          var _a;
          fullTime[col] = (_a = currentValues.value[i]) != null ? _a : 0;
        });
        // 获取当前 小时 分钟 用于判断
        const {
          hour,
          minute
        } = fullTime;
        // minTime 时间格式"10:23:34"
        if (props.minTime) {
          // 获取最小时间  时分秒
          const [minH, minM, minS] = getValidTime(props.minTime);
          minHour = minH;
          // 为最小 小时时  分钟不能小于设定值
          minMinute = +hour <= +minHour ? minM : "00";
          // 为最小 小时 分钟时  秒不能够小于设定值
          minSecond = +hour <= +minHour && +minute <= +minMinute ? minS : "00";
        }
        // 同上
        if (props.maxTime) {
          const [maxH, maxM, maxS] = getValidTime(props.maxTime);
          maxHour = maxH;
          maxMinute = +hour >= +maxHour ? maxM : "59";
          maxSecond = +hour >= +maxHour && +minute >= +maxMinute ? maxS : "59";
        }
      }
      return props.columnsType.map((type) => {
        const {
          filter,
          formatter
        } = props;
        switch (type) {
          case "hour":
            return genOptions(+minHour, +maxHour, type, formatter, filter, currentValues.value);
          case "minute":
            return genOptions(+minMinute, +maxMinute, type, formatter, filter, currentValues.value);
          case "second":
            return genOptions(+minSecond, +maxSecond, type, formatter, filter, currentValues.value);
          default:
            if (process.env.NODE_ENV !== "production") {
              throw new Error(`[Vant] DatePicker: unsupported columns type: ${type}`);
            }
            return [];
        }
      });
    });

    // 监听currentValues 更新双向绑定值
    watch(currentValues, (newValues) => {
      if (!isSameValue(newValues, props.modelValue)) {
        emit("update:modelValue", newValues);
      }
    });
    // 监听外部值 修正补零  后改变currentValues.value 值
    watch(() => props.modelValue, (newValues) => {
      newValues = formatValueRange(newValues, columns.value);
      if (!isSameValue(newValues, currentValues.value)) {
        currentValues.value = newValues;
      }
    }, {
      immediate: true
    });

    const onChange = (...args) => emit("change", ...args);
    const onCancel = (...args) => emit("cancel", ...args);
    const onConfirm = (...args) => emit("confirm", ...args);
    useExpose({
      confirm,
      getSelectedTime
    });
    return () => _createVNode(Picker, _mergeProps({
      "ref": pickerRef,
      "modelValue": currentValues.value,
      "onUpdate:modelValue": ($event) => currentValues.value = $event,
      "columns": columns.value,
      "onChange": onChange,
      "onCancel": onCancel,
      "onConfirm": onConfirm
    }, pick(props, pickerInheritKeys)), slots);
  }
});
export {
  stdin_default as default,
  timePickerProps
};
